<?php

namespace backend\controllers;

use backend\models\AdminUser;
use backend\models\form\ChangeDealUserForm;
use backend\models\form\FeedForm;
use backend\service\AuthService;
use backend\service\FeedService;
use backend\service\UserEvaluService;
use common\models\Feed;
use common\models\UserEvalu;
use common\service\ConstatService;
use common\service\Csv;
use Composer\Package\Loader\ValidatingArrayLoader;
use yii\helpers\ArrayHelper;
use yii\helpers\Url ;
use Yii;



/**
 * 评价管理
 */
class EvaluController extends BaseController
{

    /**
     * 用户评价列表
     */
    public function actionIndex(){
        $limit = 20 ;
        $searchData = Yii::$app->request->get() ;

        $orderBy = "id desc" ;
        $arr = UserEvaluService::findEvaluList($limit,$searchData,$orderBy) ;

        $users = AdminUser::findAll(["type_id"=>ConstatService::ADMIN_USER_TYPE_ID_GENERAL]) ;
        $userArr = ArrayHelper::map($users,"user_id","username") ;
        return $this->render('index', ['arr' => $arr,"limit"=>$limit,"userArr"=>$userArr]);
    }

    /**
     * 评价详情
     */
    public function actionDetail(){
        $id = Yii::$app->request->get("id");

        if(empty($id)){
            return $this->returnAjax(400,'缺少参数') ;
        }

        $evalu = UserEvalu::find()->where(['id'=>$id])->one();

        if(empty($evalu)){
            return $this->returnAjax(400,'没有该数据') ;
        }

        $feed = $evalu['feed'] ;
        $feedArr = explode("|",$feed) ;
        foreach($feedArr as $key=>$val){
            $feedArr[$key] = str_replace(":","  :  ",$val) ;
        }

        return $this->renderPartial('detail', ['evalu'=>$evalu,"feedArr"=>$feedArr]);

    }

    /**
     * 删除评价
     */
    public function actionDel(){
        $id = Yii::$app->request->get("id");

        if(empty($id)){
            return $this->returnAjax(400,'缺少参数') ;
        }

        $evalu = UserEvalu::find()->where(['id'=>$id])->one();

        if(empty($evalu)){
            return $this->returnAjax(400,'没有该数据') ;
        }

        $rs = $evalu->delete();

        if($rs){
            return $this->returnAjax(200,'删除成功', Url::toRoute("/evalu/index")) ;
        }else{
            return $this->returnAjax(400,'删除失败') ;
        }
    }

    /**
     * 导出评价
     */
    public function actionExport(){
        ini_set('memory_limit', '2048M');

        $limit = 60000;

        if (Yii::$app->request->isPost) {
            $searchData = Yii::$app->request->post() ;
        }else{
            $searchData = Yii::$app->request->get() ;
        }

        $orderBy = "id desc" ;
        $arr = UserEvaluService::exportEvalu($searchData,$orderBy) ;

        $totalCount = $arr['count'] ;

        if (Yii::$app->request->isPost) {
            if ($totalCount > $limit) {
                return $this->returnAjax(self::FAIL, "您导出的数据大于{$limit}条, 请先进行筛选!");
            }
            return $this->returnAjax(self::SUCCESS, '', Url::toRoute(["/evalu/export"]), compact('totalCount'));
        }

        if ($totalCount > $limit) {
            return $this->returnAjax(self::FAIL, "您导出的数据大于{$limit}条, 请先进行筛选!");
        }

        $name = "评价列表" ;
        $models = $arr['query'] ;
        Csv::export([
            'models' => $models,
            'fileName' => date('Y-m-d').$name,
            'columns' => ['id','user_name','upd_time','feed'],
            'headers' => [
                'id'        => '序号' ,
                'user_name' => '姓名',
                'upd_time'  => '时间',
                'feed'      => '评价',
            ]
        ], function (&$models) {
            $num = 1 ;
            foreach ($models as $key => $data) {
                $models[$key]['id'] = $num ;
                $models[$key]['feed'] = str_replace("|"," , ",$data['feed']) ;
                $models[$key]['upd_time'] = "\t" . $data['upd_time'];
                $num = $num + 1;
            }
        },true);

        Yii::$app->end();
    }


}
